package com.enhinck.dbutilweb.common.freemark.mybatisplus;

import com.enhinck.dbutilweb.common.excel.JavaDefineEntity;
import com.enhinck.dbutilweb.common.excel.JavaFieldEntity;
import com.enhinck.dbutilweb.common.freemark.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * VO工厂
 *
 * @author huenbin
 * @date 2020-07-02 17:00
 */
public class RpcControllerFactory extends BaseFacotry {
    private static final RpcControllerFactory doFactory = new RpcControllerFactory();

    public static RpcControllerFactory getInstance() {
        return doFactory;
    }


    private RpcControllerFactory() {

    }


    public JavaClassEntity create(JavaDefineEntity<JavaFieldEntity> javaDefineEntity) {
        JavaClassEntity javaClassEntity = createClass(javaDefineEntity);
        String className = getRpcControllerName(javaDefineEntity);
        javaClassEntity.setClassName(className);

        List<String> annotations = new ArrayList<>();
        annotations.add("@Slf4j");
        annotations.add("@Api(description = \"" + javaDefineEntity.getDescribe() + "管理\", tags = {\"" + className + "\"})");
        annotations.add("@RestController");
        annotations.add("@AllArgsConstructor");
        javaClassEntity.setAnnotations(annotations);


        List<String> implementsInterfaces = new ArrayList<>();
        implementsInterfaces.add(getFeignName(javaDefineEntity));
        javaClassEntity.setImplementsInterfaces(implementsInterfaces);


        Set<String> importList = new HashSet<>();

        importList.add("java.util.List");
        importList.add("lombok.*");
        importList.add("io.swagger.annotations.*");
        importList.add("lombok.extern.slf4j.Slf4j");
        importList.add("org.springframework.web.bind.annotation.*");
        importList.add(javaDefineEntity.getFeignReference());
        String responseWrapper = null;

        String pageWrapper = null;
        if (isIoc){
            importList.add("com.greentown.common.response.WebResponseEntity");
            importList.add("com.baomidou.mybatisplus.extension.plugins.pagination.Page");
            responseWrapper = "WebResponseEntity";
            pageWrapper = "Page";
        }else {
            importList.add("com.greentown.phantom.utils.DataTransformer");
            importList.add("com.greentown.phantom.common.response.WebResponse");
            importList.add("com.kingboy.common.utils.page.PageResult");
            importList.add("com.greentown.mybatisplus.util.BaseSearchVO");
            responseWrapper = "WebResponse";
            pageWrapper = "PageResult";
        }





        javaClassEntity.setImportList(importList);
        importList.addAll(javaDefineEntity.getRpcControllerImports());
        javaClassEntity.setPackagePath(javaDefineEntity.getRpcControllerPackageName());

        List<ClassField> fields = new ArrayList<>();
        ClassField classField = new ClassField();
        classField.setFieldType(getServiceName(javaDefineEntity));
        String serviceName = lowerCaseFirstChar(getServiceName(javaDefineEntity));
        classField.setFieldName(serviceName);
        classField.setClassModify("final");
        fields.add(classField);
        javaClassEntity.setFields(fields);


        List<ClassMethod> classMethods = new ArrayList<>();
        javaClassEntity.setMethods(classMethods);
        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setClassMethodDescribe("列表");
            classMethod.setMethodReturnType(responseWrapper+"<List<" + javaDefineEntity.getJavaName() + "DTO>>");
            classMethod.setMethodName("list");
            List<String> codes = new ArrayList<>();
            codes.add("return "+responseWrapper+".ok(" + serviceName + ".listDTO());");
            classMethod.setCodes(codes);
            classMethods.add(classMethod);
        }


        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper+"<Boolean>");
            classMethod.setMethodName("add");
            classMethod.setClassMethodDescribe("新增");
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@RequestBody " + javaDefineEntity.getJavaName() + "DTO");
            methodParam.setFieldName("entityDTO");
            methodParam.setClassFieldDescribe("新增对象");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            List<String> codes = new ArrayList<>();
            codes.add("return "+responseWrapper+".ok(" + serviceName + ".saveDTO(entityDTO));");
            classMethod.setCodes(codes);
            classMethods.add(classMethod);
        }

        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper+"<Boolean>");
            classMethod.setMethodName("update");
            classMethod.setClassMethodDescribe("更新");
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@RequestBody " + javaDefineEntity.getJavaName() + "DTO");
            methodParam.setFieldName("entityDTO");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            List<String> codes = new ArrayList<>();
            codes.add("return "+responseWrapper+".ok(" + serviceName + ".updateDTOById(entityDTO));");
            classMethod.setCodes(codes);
            classMethods.add(classMethod);
        }

        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper+"<" + javaDefineEntity.getJavaName() + "DTO>");
            classMethod.setMethodName("get");
            classMethod.setClassMethodDescribe("查询");
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@PathVariable(\"id\") Long");
            methodParam.setFieldName("id");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            List<String> codes = new ArrayList<>();
            codes.add("return "+responseWrapper+".ok(" + serviceName + ".getDTOById(id));");
            classMethod.setCodes(codes);
            classMethods.add(classMethod);
        }

        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper+"<Boolean>");
            classMethod.setMethodName("delete");
            classMethod.setClassMethodDescribe("删除");
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@PathVariable(\"id\") Long");
            methodParam.setFieldName("id");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            List<String> codes = new ArrayList<>();
            codes.add("return "+responseWrapper+".ok(" + serviceName + ".removeById(id));");
            classMethod.setCodes(codes);
            classMethods.add(classMethod);
        }


        return javaClassEntity;
    }


    public void write(JavaDefineEntity<JavaFieldEntity> javaDefineEntity) {
        FreemarkUtil.write(create(javaDefineEntity));
    }


}
